package com.geoway.adf.report.excel.conditionformat;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.geoway.adf.report.excel.sheet.Range;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lzw
 * @Date: 2024/1/17 17:20
 * @Description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ToString
@JsonDeserialize()
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,
        property = "type", visible = true)
@JsonSubTypes({@JsonSubTypes.Type(value = DefaultCondition.class, name = "default"),
        @JsonSubTypes.Type(value = DataBarCondition.class, name = "dataBar"),
        @JsonSubTypes.Type(value = IconCondition.class, name = "icons"),
        @JsonSubTypes.Type(value = ColorGradationCondition.class, name = "colorGradation")})
public abstract class ConditionFormat {
    /**
     * "default": 突出显示单元格规则和项目选区规则，
     *
     * "dataBar":数据条，
     *
     * "icons":图标集，
     *
     * "colorGradation": 色阶
     */
    private String type;
    /**
     * 应用的范围
     */
    private List<Range> cellrange;

    public static void main(String[] args) throws JsonProcessingException {
        String source = "[{\"type\":\"default\",\"cellrange\":[{\"row\":[2,7],\"column\":[2,2]}],\"format\":{\"textColor\":\"#000000\",\"cellColor\":\"#ff0000\"},\"conditionName\":\"betweenness\",\"conditionRange\":[{\"row\":[4,4],\"column\":[2,2]},{\"row\":[6,6],\"column\":[2,2]}],\"conditionValue\":[2,4]},{\"type\":\"dataBar\",\"cellrange\":[{\"row\":[10,15],\"column\":[10,11]}],\"format\":[\"#6aa84f\",\"#ffffff\"]},{\"type\":\"icons\",\"cellrange\":[{\"row\":[19,23],\"column\":[2,2]}],\"format\":{\"len\":\"3\",\"leftMin\":\"0\",\"top\":\"0\"}},{\"type\":\"colorGradation\",\"cellrange\":[{\"row\":[10,15],\"column\":[6,6]}],\"format\":[\"rgb(99, 190, 123)\",\"rgb(255, 235, 132)\",\"rgb(248, 105, 107)\"]}]";
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(source);
        List<ConditionFormat> conditionFormats = new ArrayList<>();
        if(jsonNode.isArray()){
            for(int i=0; i<jsonNode.size(); i++){
                JsonNode node = jsonNode.get(i);
                conditionFormats.add(mapper.treeToValue(node, ConditionFormat.class));
            }
        }
        System.out.println(conditionFormats);
    }
}
